package com.jinmdz.fmis.mapper.mapper.provider;

import com.jinmdz.fmis.mapper.entity.DocumentFileEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 公用图片及文件表Provider
 *
 * @author generator
 * @date 2020-03-12 17:08
 */
public class DocumentFileProvider {

    /**
     * 增加DocumentFile
     *
     * @param entity 数据实体
     * @return String
     * @author generator
     * @date 2020-03-12 17:08
     */
    public String insertDocumentFile(@Param("entity") DocumentFileEntity entity) {
        return new SQL() {
            {
                INSERT_INTO(" document_file ");

                if (entity.getDeleted() != null) {
                    VALUES(" deleted ", " #{entity.deleted,jdbcType=TINYINT} ");
                }

                VALUES(" table_name ", " #{entity.tableName,jdbcType=VARCHAR} ");
                VALUES(" table_id ", " #{entity.tableId,jdbcType=INTEGER} ");
                VALUES(" business_code ", " #{entity.businessCode,jdbcType=VARCHAR} ");
                VALUES(" document_type_code ", " #{entity.documentTypeCode,jdbcType=SMALLINT} ");
                VALUES(" file_name ", " #{entity.fileName,jdbcType=VARCHAR} ");
                VALUES(" file_path ", " #{entity.filePath,jdbcType=VARCHAR} ");

                if (entity.getFileLength() != null) {
                    VALUES(" file_length ", " #{entity.fileLength,jdbcType=BIGINT} ");
                }

                VALUES(" file_extension ", " #{entity.fileExtension,jdbcType=VARCHAR} ");
                VALUES(" thumb_path ", " #{entity.thumbPath,jdbcType=VARCHAR} ");
                VALUES(" file_describe ", " #{entity.fileDescribe,jdbcType=VARCHAR} ");
                VALUES(" file_code ", " #{entity.fileCode,jdbcType=VARCHAR} ");
                VALUES(" random_code ", " #{entity.randomCode,jdbcType=VARCHAR} ");
                VALUES(" remark ", " #{entity.remark} ");
                VALUES(" funeral_parlour_code ", " #{entity.funeralParlourCode,jdbcType=VARCHAR} ");
                VALUES(" create_user_id ", " #{entity.createUserId,jdbcType=INTEGER} ");

                if (entity.getUnmodified() != null) {
                    VALUES(" unmodified ", " #{entity.unmodified,jdbcType=TINYINT} ");
                }

                VALUES(" modified_user_id ", " #{entity.modifiedUserId,jdbcType=INTEGER} ");
                VALUES(" version ", " unix_timestamp(now()) ");
            }
        }.toString();
    }

    /**
     * 修改DocumentFile
     *
     * @param entity 数据实体
     * @return String
     * @author generator
     * @date 2020-03-12 17:08
     */
    public String updateDocumentFile(@Param("entity") DocumentFileEntity entity) {
        return new SQL() {
            {
                UPDATE(" document_file ");

                if (entity.getDeleted() != null) {
                    SET(" deleted = #{entity.deleted,jdbcType=TINYINT} ");
                }

                SET(" table_name = #{entity.tableName,jdbcType=VARCHAR} ");
                SET(" table_id = #{entity.tableId,jdbcType=INTEGER} ");
                SET(" business_code = #{entity.businessCode,jdbcType=VARCHAR} ");
                SET(" document_type_code = #{entity.documentTypeCode,jdbcType=SMALLINT} ");
                SET(" file_name = #{entity.fileName,jdbcType=VARCHAR} ");
                SET(" file_path = #{entity.filePath,jdbcType=VARCHAR} ");

                if (entity.getFileLength() != null) {
                    SET(" file_length = #{entity.fileLength,jdbcType=BIGINT} ");
                }

                SET(" file_extension = #{entity.fileExtension,jdbcType=VARCHAR} ");
                SET(" thumb_path = #{entity.thumbPath,jdbcType=VARCHAR} ");
                SET(" file_describe = #{entity.fileDescribe,jdbcType=VARCHAR} ");
                SET(" file_code = #{entity.fileCode,jdbcType=VARCHAR} ");
                SET(" random_code = #{entity.randomCode,jdbcType=VARCHAR} ");
                SET(" remark = #{entity.remark} ");
                SET(" funeral_parlour_code = #{entity.funeralParlourCode,jdbcType=VARCHAR} ");

                if (entity.getUnmodified() != null) {
                    SET(" unmodified = #{entity.unmodified,jdbcType=TINYINT} ");
                }

                SET(" modified_user_id = #{entity.modifiedUserId,jdbcType=INTEGER} ");
                SET(" version = unix_timestamp(now()) ");
                WHERE(" id = #{entity.id,jdbcType=INTEGER} ");
                WHERE(" unmodified = 0 ");
                WHERE(" version = #{entity.version,jdbcType=INTEGER} ");
            }
        }.toString();
    }

    /**
     * 查询DocumentFile列表
     *
     * @param entity 数据实体
     * @return String
     * @author generator
     * @date 2020-03-12 17:08
     */
    public String listDocumentFile(@Param("entity") DocumentFileEntity entity) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" id,deleted,table_name,table_id,business_code,document_type_code,file_name,file_path,file_length,file_extension,thumb_path,file_describe,file_code,random_code,remark,funeral_parlour_code,create_user_id,gmt_create,unmodified,modified_user_id,gmt_modified,version ");
                SELECT(builder.toString());
                FROM(" document_file ");

                if (entity.getTableName() != null) {
                    WHERE(" table_name = #{entity.tableName,jdbcType=VARCHAR} ");
                }

                if (entity.getTableId() != null) {
                    WHERE(" table_id = #{entity.tableId,jdbcType=INTEGER} ");
                }

                if (entity.getBusinessCode() != null) {
                    WHERE(" business_code = #{entity.businessCode,jdbcType=VARCHAR} ");
                }

                if (entity.getDocumentTypeCode() != null) {
                    WHERE(" document_type_code = #{entity.documentTypeCode,jdbcType=SMALLINT} ");
                }

                if (entity.getFileName() != null) {
                    WHERE(" file_name = #{entity.fileName,jdbcType=VARCHAR} ");
                }

                if (entity.getFilePath() != null) {
                    WHERE(" file_path = #{entity.filePath,jdbcType=VARCHAR} ");
                }

                if (entity.getFileLength() != null) {
                    WHERE(" file_length = #{entity.fileLength,jdbcType=BIGINT} ");
                }

                if (entity.getFileExtension() != null) {
                    WHERE(" file_extension = #{entity.fileExtension,jdbcType=VARCHAR} ");
                }

                if (entity.getThumbPath() != null) {
                    WHERE(" thumb_path = #{entity.thumbPath,jdbcType=VARCHAR} ");
                }

                if (entity.getFileDescribe() != null) {
                    WHERE(" file_describe = #{entity.fileDescribe,jdbcType=VARCHAR} ");
                }

                if (entity.getFileCode() != null) {
                    WHERE(" file_code = #{entity.fileCode,jdbcType=VARCHAR} ");
                }

                if (entity.getRandomCode() != null) {
                    WHERE(" random_code = #{entity.randomCode,jdbcType=VARCHAR} ");
                }

                if (entity.getRemark() != null) {
                    WHERE(" remark = #{entity.remark} ");
                }

                if (entity.getFuneralParlourCode() != null) {
                    WHERE(" funeral_parlour_code = #{entity.funeralParlourCode,jdbcType=VARCHAR} ");
                }

            }
        }.toString();
    }
}